﻿namespace Dissemination.MVC.Controllers
{
    using System.Web.Mvc;
    using Repositories;
    using Models;
    using System;

    public class GenderController : ControllerBase
    {
        protected GenderRepository Repository = new GenderRepository();

        #region List

        //
        // GET: /Gender/

        /// <summary>
        /// Returns the view associated to the list of entities in the data source.
        /// </summary>
        /// <returns>Index view</returns>
        [Authorize(Roles = "Demostrator,Administrator")]
        public ActionResult Index()
        {
            if (log.IsDebugEnabled) log.Debug("Entering Genders list...");
            if (log.IsDebugEnabled) log.Debug("Leaving Genders list...");
            return View(Repository.List());
        }

        #endregion

        #region Details

        //
        // GET: /Gender/Details/5

        /// <summary>
        /// Returns the view associated to the details of an entity in the data source.
        /// </summary>
        /// <param name="id">The entity id.</param>
        /// <returns>Details view</returns>
        [Authorize(Roles = "Demostrator,Administrator")]
        public ActionResult Details(int id)
        {
            if (log.IsDebugEnabled) log.DebugFormat("Entering Gender details with id={0}...", id);
            Gender gender = Repository.Get(id);
            
            if (gender == null)
            {
                if (log.IsErrorEnabled) log.ErrorFormat("Gender with id={0} not found", id);
                return View("NotFound");
            }

            if (log.IsDebugEnabled) log.DebugFormat("Leaving Gender details with id={0}...", id);
            return View(gender);
        }

        #endregion

        #region New

        //
        // GET: /Gender/Create

        /// <summary>
        /// Returns the view associated with an empty entity
        /// </summary>
        /// <returns>Create view</returns>
        [Authorize(Roles = "Administrator")]
        public ActionResult Create()
        {
            if (log.IsDebugEnabled) log.Debug("Entering Gender creation (GET)...");
            if (log.IsDebugEnabled) log.Debug("Leaving Gender creation (GET)...");
            return View(new Gender());
        } 

        //
        // POST: /Gender/Create

        /// <summary>
        /// Creates the specified entity.
        /// </summary>
        /// <param name="Gender">The entity</param>
        /// <returns>Details view</returns>
        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public ActionResult Create(Gender gender)
        {
            if (log.IsDebugEnabled) log.Debug("Entering Gender creation (POST)..."); 
            if (ModelState.IsValid)
            {
                Repository.Add(gender);
                Repository.Save();

                if (log.IsDebugEnabled) log.Debug("Leaving Gender creation (POST)...");
                return RedirectToAction("Details", new { id = gender.GenderID });
            }

            if (log.IsDebugEnabled)
            {
                //TODO: completar Create_Gender
            } 
            
            return View(gender);
        }

        #endregion

        #region Edit

        //
        // GET: /Gender/Edit/5

        /// <summary>
        /// Returns the view associated with the edit of an existing entity.
        /// </summary>
        /// <param name="id">The id of the entity to edit</param>
        /// <returns>Edit view</returns>
        [Authorize(Roles = "Administrator")]
        public ActionResult Edit(int id)
        {
            if (log.IsDebugEnabled) log.DebugFormat("Entering Gender with id {0} edit (GET)...", id);
            Gender gender = Repository.Get(id);
            
            if (gender == null)
            {
                if (log.IsErrorEnabled) log.ErrorFormat("Gender with id={0} not found", id);
                return View("NotFound");
            }

            if (log.IsDebugEnabled) log.DebugFormat("Leaving Gender with id {0} edit (GET)...", id);
            return View(gender);
        }

        //
        // POST: /Gender/Edit/5

        /// <summary>
        /// Edits the specified entity.
        /// </summary>
        /// <param name="id">The id of the existing entity.</param>
        /// <param name="collection">The collection of values entered.</param>
        /// <returns>Details view</returns>
        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public ActionResult Edit(int id, FormCollection collection)
        {
            if (log.IsDebugEnabled) log.DebugFormat("Entering Gender with id {0} edit (POST)...", id);
            Gender gender = Repository.Get(id);

            try
            {
                UpdateModel(gender);
                Repository.Save();

                if (log.IsDebugEnabled) log.DebugFormat("Leaving Gender with id {0} edit (POST)...", id);
                return RedirectToAction("Details", new { id = gender.GenderID });
            }
            catch (Exception ex)
            {
                if (log.IsErrorEnabled) log.Error("Error editing Gender", ex);
                //ModelState.AddRuleViolations(Gender.GetRuleViolations());
                return View(gender);
            }
        }

        #endregion

        #region Delete

        //
        // GET: /Gender/Delete/5

        /// <summary>
        /// Returns the view associated with a given entity to be deleted
        /// </summary>
        /// <param name="id">The id of the existing entity</param>
        /// <returns>Delete view</returns>
        [Authorize(Roles = "Administrator")]
        public ActionResult Delete(int id)
        {
            throw new NotImplementedException();

            if (log.IsDebugEnabled) log.DebugFormat("Entering Gender with id {0} delete (GET)...", id);
            Gender gender = Repository.Get(id);

            if (gender == null)
            {
                if (log.IsErrorEnabled) log.ErrorFormat("Gender with id={0} not found", id);
                return View("NotFound");
            }

            if (log.IsDebugEnabled) log.DebugFormat("Leaving Gender with id {0} delete (GET)...", id);
            return View(gender);
        }

        //
        // POST: /Gender/Delete/5

        /// <summary>
        /// Deletes the specified entity.
        /// </summary>
        /// <param name="id">The id of the existing entity</param>
        /// <param name="confirmButton">The confirm button.</param>
        /// <returns>Deleted view</returns>
        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public ActionResult Delete(int id, string confirmButton)
        {
            throw new NotImplementedException();

            if (log.IsDebugEnabled) log.DebugFormat("Entering Gender with id {0} delete (POST)...", id);
            Gender gender = Repository.Get(id);

            if (gender == null)
            {
                if (log.IsErrorEnabled) log.ErrorFormat("Gender with id={0} not found", id);
                return View("NotFound");
            }

            Repository.Delete(gender);
            Repository.Save();

            if (log.IsDebugEnabled) log.DebugFormat("Leaving Gender with id {0} delete (POST)...", id);
            return View("Deleted");
        }

        #endregion

    }
}
